package com.cn.boot.student.service.student.impl;

import com.cn.boot.student.entity.SignEntity;
import com.cn.boot.student.entity.UserEntity;
import com.cn.boot.student.mapper.SignMapper;
import com.cn.boot.student.mapper.UserMapper;
import com.cn.boot.student.service.common.impl.RedisServiceImpl;
import com.cn.boot.student.service.student.ISignService;
import com.cn.boot.student.util.CommonResult;
import com.cn.boot.student.util.Tool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


/**
 * 签到逻辑实现层
 */
@Service
@CacheConfig(cacheNames = "sign")
public class SignServiceImpl implements ISignService {
    @Autowired
    private SignMapper signMapper;
    @Autowired
    private RedisServiceImpl redisService;

    /**
     * 签到
     *
     * @param signEntity
     * @return
     */

    @Override
    public CommonResult sign(SignEntity signEntity) {
        List<UserEntity> list =signMapper.signByUserId(signEntity.getUserId());
        if(list.size() == 1){
            return CommonResult.success("isSign","不能重复签到！！！");
        }
        if(signEntity.getLateTime() == null || signEntity.getLateTime().equals("")){
            Integer result = signMapper.insert(signEntity);
            if (result == 0) {
                return CommonResult.success(result, "签到失败！！！");
            }else{
                return CommonResult.success(result, "签到成功！！！");
            }
        }else{
            Integer result = signMapper.add(signEntity);
            if (result == 0) {
                return CommonResult.success(result, "签到失败！！！");
            }else{
                return CommonResult.success(result, "签到成功！！！");
            }
        }
    }

    /**
     * 查询签到状态
     *
     * @param userEntity
     * @return
     */
    @Override
    public CommonResult findAll(UserEntity userEntity) {
        //得到查询总数
        Integer count = signMapper.getCount(userEntity);
        //分页，条件查询
        PageHelper.startPage(Tool.toPageNum(userEntity.getPageNum()), Tool.toPageSize(userEntity.getPageSize()));
        List<UserEntity> list = signMapper.findAll(userEntity);
        PageInfo<UserEntity> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("pageInfo", pageInfo);
        return CommonResult.success(map, "查询成功！！！");
    }
}
